package club.baimahu.pencil;

import club.baimahu.pencil.domain.entity.Goods;
import club.baimahu.pencil.domain.entity.discount.CommonCondition;
import club.baimahu.pencil.domain.entity.discount.DiscountManager;
import club.baimahu.pencil.domain.entity.discount.FixDiscountStrategy;
import club.baimahu.pencil.domain.entity.discount.PercentDiscountStrategy;
import club.baimahu.pencil.domain.entity.order.Commodity;
import club.baimahu.pencil.domain.entity.order.CommodityList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * 1.将优惠券的机制抽象成一个个算法策略,用户可以自由选择使用哪张优惠券,或由系统自动计算,选用优惠力度最大的优惠券.
 * 2.并且券还分有门槛和无门槛类型,有门槛需达到条件才能生效,不然只能按原价购买
 * PS: 当前优惠机制暂不支持优惠券的叠加
 */
public class Story4 {
    static Logger logger = LoggerFactory.getLogger(Story4.class);


    /**
     * 如商品订单大于某总价时，可以优惠减免，
     * 优惠减免包括按打几折算和固定扣减金额算。优惠减免必须满足上述条件，而条件可能是满足特定数量或特定总价，或者既要满足特定数量又要满足特定金额，此时使用过滤器模式实现条件的判断
     * @param args
     */
    public static void main(String[] args) {


        CommonCondition condition = CommonCondition.orCondition(5,new BigDecimal(100));
        CommodityList commodityList = new CommodityList();
        Commodity commodity = new Commodity();

        Goods goods = new Goods("0004","铅笔",new BigDecimal(2),new BigDecimal(4));
        commodity.setGoods(goods);
        commodityList.setCommodityList(Arrays.asList(commodity));

        DiscountManager discountManager = new DiscountManager();
        discountManager.setDiscountStrategyList(Arrays.asList(new PercentDiscountStrategy(0.8,condition),new FixDiscountStrategy(new BigDecimal(25),condition)));
        discountManager.setCommodityList(commodityList);


        logger.info("{}售价{}元",goods.getName(),goods.getSellPrice());

        commodity.setAmount(2);
        BigDecimal optimalPay = discountManager.getOptimalPayment();
        logger.info("优惠券需满足大于100元或数量大于5件，购买铅笔{}只，应付 {} 元",commodity.getAmount(),optimalPay.doubleValue());

        commodity.setAmount(25);
        optimalPay = discountManager.getOptimalPayment();
        logger.info("优惠券需满足大于100元或数量大于5件，购买铅笔{}只，应付 {} 元",commodity.getAmount(),optimalPay.doubleValue());

        discountManager.setDiscountStrategyList(Arrays.asList(new PercentDiscountStrategy(0.8,condition)));
        optimalPay = discountManager.getOptimalPayment();
        logger.info("优惠券打八折，需满足条件数量大于5件，购买铅笔{}只，应付 {} 元",commodity.getAmount(),optimalPay.doubleValue());

        condition = CommonCondition.andCondition(26,new BigDecimal(100));
        discountManager.setDiscountStrategyList(Arrays.asList(new PercentDiscountStrategy(0.8,condition),new FixDiscountStrategy(new BigDecimal(25),condition)));
        optimalPay = discountManager.getOptimalPayment();
        logger.info("优惠券需满足大于100元并且数量大于26件，购买铅笔{}只，应付 {} 元",commodity.getAmount(),optimalPay.doubleValue());


        discountManager.setDiscountStrategyList(Arrays.asList(new PercentDiscountStrategy(0.8)));
        optimalPay = discountManager.getOptimalPayment();
        logger.info("无门槛打折优惠券，购买铅笔{}只，应付 {} 元",commodity.getAmount(),optimalPay.doubleValue());




/*        BigDecimal money = new BigDecimal(100);
        DiscountStrategy strategy1 = new FixDiscountStrategy(new BigDecimal(10));

        DiscountStrategy strategy2 = new PercentDiscountStrategy(0.8);


        logger.info("固定金额满减,最终优惠价={}",strategy1.discount(money));
        logger.info("打折满减,最终优惠价={}",strategy2.discount(money));*/


    }
}
